package com.ding.dailyup.redis;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;

import cn.hutool.core.util.ObjectUtil;

@Component
public class RedisService {
	private static Logger logger = LoggerFactory.getLogger(RedisService.class);

	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RedisDao redisDao;

	// 10分钟
	private long HOT_INDUSTRY_DATA_KEEP_TIME = 1000 * 60 * 10;

	/**
	 * 返回是不是当日第一次登陆
	 *
	 * @param key
	 * @param item
	 * @param score
	 * @return
	 */
	public boolean dailyFirstLogin(String key, String item, long score) {
		ZSetOperations zSetOperations = redisTemplate.opsForZSet();
		Double s = zSetOperations.score(key, item);
		boolean flag = false;
		if (ObjectUtil.isNull(s) || s == 0) {
			flag = true;
			zSetOperations.add(key, item, score);
		}
		redisTemplate.expire(key, 8, TimeUnit.DAYS);
		return flag;
	}

	/**
	 * 设定最大上传股票时间
	 */
	public void setMaxRecommendTime(long timeStamp) {
		redisDao.set(RedisCacheKey.MAX_RECOMMEND_TIME, timeStamp);
	}

	/**
	 * 获取最大上传股票时间
	 * 
	 * @return
	 */
	public long getMaxRecommendTime() {
		Object o = redisDao.get(RedisCacheKey.MAX_RECOMMEND_TIME);
		if (ObjectUtil.isNull(o)) {
			long timestamp = LocalDateTime
					.of(LocalDate.now(), LocalTime.of(0, 0, 0))
					.toEpochSecond(ZoneOffset.of("+8")) * 1000;
			this.setMaxRecommendTime(timestamp);
			return timestamp;
		} else {
			return Long.parseLong(o.toString());
		}
	}

	public JSONArray getHotIndustry(int dayType) {
		String dataKey = String.format(RedisCacheKey.HOT_INDUSTRY_KEY, dayType);
		String timeKey = RedisCacheKey.HOT_INDUSTRY_TIME_KEY;
		// 先从缓存里面取时间
		Double originTimeStamp = redisDao.getScoreZSet(timeKey,
				String.valueOf(dayType));
		JSONArray rs = null;
		if (ObjectUtil.isNotEmpty(originTimeStamp) && originTimeStamp != 0) {
			// 有值的话 , 还需要判断时间
			int hour = LocalDateTime.now().getHour();
			long currentTimeMillis = System.currentTimeMillis();

			if (hour > 8 && hour < 16) {
				// 交易时间 需要 正常更新
				// 如果值还在时效性中, 就用缓存中的值
				if (currentTimeMillis - originTimeStamp
						.longValue() < HOT_INDUSTRY_DATA_KEEP_TIME) {
					rs = JSONArray.parseArray(redisDao.get(dataKey).toString());
				}
			} else {
				// 非交易时间, 直接返回原来的值
				rs = JSONArray.parseArray(redisDao.get(dataKey).toString());
			}

		}
		return rs;
	}

	public void setHotIndustry(int dayType, JSONArray data) {
		String dataKey = String.format(RedisCacheKey.HOT_INDUSTRY_KEY, dayType);
		String timeKey = RedisCacheKey.HOT_INDUSTRY_TIME_KEY;
		redisDao.addZset(timeKey, String.valueOf(dayType),
				System.currentTimeMillis());
		redisDao.set(dataKey, data.toJSONString());
	}
}
